//
//  DataTypeView.swift 基本数据类型
//  swiftStudy
//
//  Created by Zhang on 2021/3/17.
//

import SwiftUI

struct DataTypeView: View {
    
    let code_1:String
    let code_2:String
    let code_3:String
    
    init(){
        code_1 =
            "var type_10 = 17 //十进制的17\n" +
            "var type_2 = 0b10001 //二进制的17\n" +
            "var type_8 = 0o21 //八进制的17\n" +
            "var type_16 = 0x11 //16进制的17\n" +
            "\n" +
            "print(\"\\(type_10),\\(type_2),\\(type_8),\\(type_16)\")"
        
        code_2 =
            "//创建元组数据类型\n" +
            "var person:(name:String,age:Int) = (\"Zhang\",30)\n" +
            "//获取元组数据\n" +
            "var name = person.name\n" +
            "var age = person.age\n" +
            "//统一获取\n" +
            "var (name_1,age_1) = person\n" +
            "print(name_1,age_1)"
        
        code_3 =
            "//在声明常量和变量的时候,在类型后面加?,就可以声明为Optional类型\n" +
            "var str:String?\n" +
            "let name_2:String?\n" +
            "\n" +
            "str = \"Zhang\"\n" +
            "name_2 = \"Zhang\"\n" +
            "\n" +
            "if(str != nil){\n" +
            "    //如果要取出Optional类型的值,需要在变量后面加!,即为对Optional类型进行拆包\n" +
            "    print(str!,name_2!)\n" +
            "}\n" +
            "\n" +
            "//if-let语法拆包,如果str不为nil则把值赋值给tem并执行分支里的方法,否则不执行\n" +
            "if let tem = str{\n" +
            "    print(tem)\n" +
            "}"
    }
    
    var body: some View {
        ScrollView(.vertical){
            
            VStack(alignment:.leading, spacing: 5){
                Section(header: SectionHeaderView(title:"简单介绍")){
                    CommonTextView(content: "Swift语言支持使用多种进制进行数据的定义与计算,默认为十进制,可以通过在数据前面加前缀实现,如下:")
                    
                    CodeView(code: code_1).padding(10)
                }
                
                Section(header: SectionHeaderView(title:"特殊数据类型")){
                    CommonTextView(content: "Swift语言中还支持两种特殊的基本数据类型,分别是元组类型和可选值类型.")
                    CommonTextView(content: "\"元组\"允许一些并不相关的类型进行自由组合成为新的集合类型,例如:")
                    
                    CodeView(code: code_2).padding(10)
                    
                    CommonTextView(content: "\"可选值类型\"就是Swift语言中空安全的实现方式,在Swift语言中,如果使用了一个没有进行赋值的变量,程序在编译期就会检测到,并给出错误提示,一个普通变量默认是不可以为空的,如果需要需要定义一个可为空的变量,需要将其包装为Optional类型,例:")
                    CodeView(code: code_3).padding(10)
                    
                }
            }
            //设置VStack,横向填充屏幕
            .frame(minWidth: 0,
                   maxWidth: .infinity,
                   minHeight: 0,
                   maxHeight: .infinity,
                   alignment: .topLeading
            )
        }
    }
}

struct DataTypeView_Previews: PreviewProvider {
    static var previews: some View {
        DataTypeView()
    }
}
